<template>
  <div class="app-container">
    <el-form ref="form" :model="record" label-width="120px">
      <el-form-item label="借阅人">
        <el-input v-model="student.sname"/>
        <el-input v-model="record.caid" type="hidden"/>
      </el-form-item>
      <el-form-item label="书籍名称">
        <el-input v-model="book.name"/>
        <el-input v-model="record.bid" type="hidden"/>
      </el-form-item>
      <el-form-item label="借书日期">
        <el-date-picker
          v-model="record.bdate"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="请选择时间"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item label="还书日期">
        <el-date-picker
          v-model="record.rdate"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="请选择时间"
          style="width: 100%"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary"  @click="onSubmit">新增借阅记录</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import request from '@/utils/request'
export default {
  data() {
    return {
      record: {
        caid: '',
        bid: '',
        bdate: '',
        rdate: ''
      },
      student: {
        sname: ''
      },
      book: {
        name: '',
        caid:'',
        bid:'',
        bdate: '',
        rdate: ''
      },
    }
  },
  methods: {
    onSubmit() {
      request({
        url: "/edu/student/selectIdByName.do?name=" + this.student.sname,
        method: "GET"
      }).then(res => {
        if (res.code == 0) {
          this.record.caid = res.data.id;

          request({
            url: "/library/book/selectIdByName.do?name=" + this.book.name,
            method: "GET"
          }).then(res => {
            if (res.code == 0) {
              this.record.bid = res.data.id;

              request({
                url: "/library/record/save.do",
                method: "POST",
                data: this.record
              }).then(res => {
                if (res.code == 0) {
                  //跳转到列表页面
                  this.$router.push("/recordlist");
                }
              });
            }
          });
        }
      });
      // 请求接口 实现用户新增接口的调用

    },
    onCancel() {
      this.$message({
        message: '取消新增借阅',
        type: 'warning'
      })
    }
  }
}
</script>

<style scoped>
.line {
  text-align: center;
}
</style>

